import re
import time
import threading
import jieba
import pyautogui
import pyperclip

import power
from htm import htm_info
from resust import kai_jiang
from wxclick import *
from wxauto import *
#from init import *
from RWdata import *

DEBUG=True
def debug_print(*p):
    if DEBUG:
        print(*p)
def init_ben_qi_shiJian():
    global allTime
    global localAllTime
    global oldTime

    readMin, readSec, times = htm_info()
    curTime = time.localtime()
    allTime = readMin * 60 + readSec
    localAllTime = curTime.tm_min * 60 + curTime.tm_sec
    oldTime.append(times[0].string)
    oldTime.append(times[1].string)
    oldTime.append(times[2].string)

    debug_print("网站读出来的时间", readMin, readSec, times)
    debug_print("当前系统的时间：", curTime.tm_hour, curTime.tm_min, curTime.tm_sec)

    return readMin, readSec, times

#开辟一个定时器来显示时间
def countdown():
    global readMin
    global readSec
    global localSec
    global allTime
    global localAllTime
    global oldTime
    global canDo

    userTime =1
    kaiJiangFlag =True #当为flase的时候，说明完成了开奖，准备到下一期

    curTime=time.localtime()
    nowTime= curTime.tm_min * 60 + curTime.tm_sec
    if localAllTime>nowTime:    #当过小时的时候
        countTime=allTime-(60*60-localAllTime+nowTime)
    else:
        countTime =allTime+localAllTime-nowTime
    if countTime<35:
        canDo= False
    #===============test start
    '''times=['3076742', '12-05 22:11:00', '5+1+5=11']
    kaiJiang=['小', '单', '小单']

    jieSuna('./Data/回龙12-5.xlsx', times, kaiJiang, 11)'''

    #==============================


    debug_print('下期剩余时间',countTime,int(countTime/60),":",countTime%60)
    if countTime<1:
        while kaiJiangFlag:
            time.sleep(8)
            readMin, readSec, times = htm_info()
            debug_print("准备出结果",times,oldTime)

            if oldTime[0] != times[0].string:
                debug_print('现在已开奖')
                #oldTime = times[0].string
                data =times[2].string
                data =data[-2:]
                data =int(data)
                kaiJiang=kai_jiang(data)
                print('开奖的结果是：',data,kaiJiang)
                #准备结算
                curTime = time.localtime()
                jieSuna( times, kaiJiang, data,curTime)
                kaiJiangFlag =False
                canDo =True
                #进行初始化，准备下一期
                allTime = readMin * 60 + readSec

                localAllTime = curTime.tm_min * 60 + curTime.tm_sec
                debug_print("准备下一期的时间", readMin, readSec)
                oldTime[0] = times[0].string
                oldTime[1] = times[1].string
                oldTime[2] = times[2].string

    timer = threading.Timer(userTime, countdown)
    timer.start()
#========================================================开始了程序 ======================
#定义分，秒
minutes =0 #存储读出来的分
seconds =0
times =0    #期数

local_min =0   #上一次读出的分钟
localSec =0   #上一次读出的秒
allTime =0 #记录从网上读出的时间
localAllTime =0 #记录本地时间
oldTime =[] #记录开奖前的基数
haveFile =False #用来判断文件是否存在
canDo =True #用来判断是否还可以上分
jieSuanFlag =False #用来判断是否可以结算

init()
init_ben_qi_shiJian()
jieba.set_dictionary("./img/words.txt")

#kaiJiang=kai_jiang(data)
#debug_print("开奖结果",kaiJiang)

#debug_print("网站读出来的时间",readMin,readSec,times)

timer =threading.Timer(1,countdown)
timer.start()
while True:
    try:
        comeXinxi = click_picture('./img/new1.png')
        #pyautogui.PAUSE = 2
        if comeXinxi:
            fous = click_picture('./img/input.png')
            if fous:
                wx = WeChat()
                msgs = wx.GetLastMessage
                debug_print("读出微信内容", msgs[0], msgs[1])
                wx_info=msgs[1]
                wx_name=msgs[0]
                if msgs[0]=='cmd':#读到的是命令的微信，主的的命令有：上分、下分、
                    power.cmd_caoZuo(wx_name,wx_info)
                else:#当不是命令的时候
                    if canDo:#没有达到倒数的35S
                        '''wx = WeChat()
                        msgs = wx.GetLastMessag
                        debug_print("读出微信内容", msgs[0], msgs[1])'''
                        curTime = time.localtime()
                        if 1:
                            fileName = './Data/' + msgs[0] + str(curTime.tm_mon) + '-' + str(curTime.tm_mday) + ".xlsx"
                            files = read_files()
                            for i in files:
                                if i == fileName:
                                    haveFile = True
                                    debug_print("有这个人今天的文件", fileName)
                                    input_data(msgs[1], oldTime, fileName,"下分")
                                    break
                            if haveFile == False:
                                debug_print("没有文件，新建文件",fileName)
                                new_file(msgs[0])
                                input_data(msgs[1], oldTime, fileName,"下分")
                                haveFile = False

                    else:
                        debug_print("=============================现在已不可以上分了")
                        if msgs[1]=='查询':
                            input_data(msgs[1], oldTime, fileName, "查询")
                        else:
                            pyperclip.copy("请期待下一轮的攻击")
                            pyautogui.hotkey('ctrl', 'v')
                            #pyautogui.PAUSE=1
                            pyautogui.hotkey('enter')
        #time.sleep(3)
    except:
        pyautogui.ImageNotFoundException